package com.goldze.mvvmhabit.ui.rvgroup;

import android.app.Application;
import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.widght.expand.ExpandItemBinding;
import com.goldze.mvvmhabit.widght.expand.ExpandOnItemBind;
import com.goldze.mvvmhabit.widght.expand.ExpandableViewModel;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * create om  2020/11/16.
 * Created by  gaoxuge
 * email android_gaoxuge@163.com
 * 功能描述
 */
public class ExPandRvViewModel extends BaseViewModel {
    public ObservableField<String> rightTitleLabel= new ObservableField<>("");
    public ObservableField<Boolean> showEmptyViewtest = new ObservableField<>(true);
    public ExPandRvViewModel(@NonNull Application application) {
        super(application);
        showEmptyViewtest.set(false);
        rightTitleLabel.set("添加");

    }

    public BindingCommand onRightTitleLabelClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            addItem();
        }
    });
    public void initData(){
        list.clear();
        for (int i = 0; i < 5; i++) {
            String text = "我是第" + i + "条";
            ExpandParentViewModel item = new ExpandParentViewModel(i, text);
            item.initData();
            list.add(item);
        }
    }

    public void addItem(){
        ExpandableViewModel expandableViewModel = list.get(list.size() - 1);
        ExpandParentViewModel item = new ExpandParentViewModel(expandableViewModel.getId()+1, "我是第"+(expandableViewModel.getId()+1)+"条");
        list.add(item);
    }
    public ObservableList<ExpandableViewModel> list = new ObservableArrayList<>();

    //RecyclerView多布局添加ItemBinding
    public ExpandItemBinding<ExpandParentViewModel> groupItemBinding = ExpandItemBinding.of(new ExpandOnItemBind<ExpandParentViewModel>() {
        @Override
        public void onItemBind(ExpandItemBinding binding, int position, ExpandParentViewModel item) {
            KLog.e("sss   ");
            //设置左布局
            binding.set(BR.viewModel, R.layout.recycler_group_item);
        }

        @Override
        public void onItemBind(ExpandItemBinding itemBinding, int groupPosition, int childPosition, ExpandParentViewModel item) {
            KLog.e("sss   ");
        }
    });
    public ExpandItemBinding<ExpandChildViewModel> childItemBinding = ExpandItemBinding.of(new ExpandOnItemBind<ExpandChildViewModel>() {
        @Override
        public void onItemBind(ExpandItemBinding itemBinding, int position, ExpandChildViewModel item) {
            KLog.e("sss   ");
        }

        @Override
        public void onItemBind(ExpandItemBinding itemBinding, int groupPosition, int childPosition, ExpandChildViewModel item) {
            //通过item的类型, 动态设置Item加载的布局
            KLog.e("sss   ");
            //设置左布局
            itemBinding.set(BR.viewModel, R.layout.recycler_item);
        }
    });

}
